﻿using System.Collections.Generic;
using FastNET.CleanAct.Client.ManagerEngineBusiness.DataObjects;
using FastNET.CleanAct.Common.Configuration.Configuration;
using FastNET.CleanAct.Common.EngineModel.Managers.Interface;
using FastNET.CleanAct.Common.Repository;

namespace FastNET.CleanAct.Client.ManagerEngine.Managers
{
    /// <summary>
    /// ServerDataManager class for the Control Engine
    /// </summary>
    internal class ServerDataManager : IServerDataManager
    {

        /// <summary>
        /// Gets all the templates under the specified schedule ID
        /// </summary>
        /// <param name="scheduleID">The Schedule ID to retrieve all the templates by</param>
        /// <returns>IList of all Identities for all the templates associated with the schedule</returns>
        public IList<Identity> GetScheduleTemplates(Identity scheduleID)
        {
            IList<Identity> returnList = new List<Identity>();

            Schedule schedule = new Schedule(scheduleID);

            AddSubTemplates(schedule.TemplateID, returnList);

            return returnList;
        }


        private static void AddSubTemplates(Identity parentTemplateID, IList<Identity> templateIDs)
        {
            IList<TemplateGroup> groups = TemplateGroup.Retrieve(parentTemplateID);

            foreach (TemplateGroup group in groups)
            {
                if (!templateIDs.Contains(group.ChildTemplateID))
                    templateIDs.Add(group.ChildTemplateID);

                AddSubTemplates(group.ChildTemplateID, templateIDs);
            }
        }


        /// <summary>
        /// Pulls a list of actions from the database for all the template IDs specified and returns a XML structure for client processing.
        /// </summary>
        /// <param name="templateIDs">The list of all Template IDs the instruction set is to contain</param>
        /// <returns>XML structure for Client side processing of actions</returns>
        public string BuildInstructionSet(IList<Identity> templateIDs)
        {
           JobConfigSettings settings = new JobConfigSettings();

            foreach (Identity templateID in templateIDs)
            {
                Template template = new Template(templateID);

                IList<TemplateAction> actions = TemplateAction.RetrieveByTemplateID(templateID);
                JobItem job = new JobItem {Name = template.Name}; //TODO Description

                settings.Jobs.Add(job);

                foreach (TemplateAction action in actions)
                {
                    job.Actions.Add(new ActionItem
                                             {
                                                 Name = action.Name,
                                                 RecursiveMode = (byte) action.RecursiveMode,
                                                 Type = action.ActionType,
                                                 Value = action.Pattern
                                             });
                }
            }

            return JobConfigSettings.WriteXML(settings);
        }


    }
}
